package com.echo.blog.business.sys.controller;

import com.echo.blog.business.sys.model.SysUser;
import com.echo.blog.business.sys.service.ISysUserService;
import com.echo.model.constant.response.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chentl123
 * @since 2019-04-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {


    @Resource
    private ISysUserService sysUserService;

    @RequestMapping(value = "/update")
    public ResponseData updateUserInfo(SysUser sysUser) {
        if (sysUser == null) {
            return ResponseData.error("传入的用户对象为空");
        }
        sysUserService.updateById(sysUser);
        return ResponseData.success(200, "获取用户基础信息成功", null);
    }

    @RequestMapping(value = "/getUserInfo")
    public ResponseData getUserInfo(String username) {
        log.info("我要获取名称为: " + username + " 的用户信息");
        SysUser sysUser = sysUserService.getUserByName(username);
        if (sysUser == null) {
            return ResponseData.error("未找到对应账户名称的信息");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id", sysUser.getId());
        resultMap.put("name", sysUser.getName());
        resultMap.put("username", sysUser.getUsername());
        resultMap.put("avatar", sysUser.getAvatar());
        resultMap.put("age", sysUser.getAge());
        resultMap.put("signature", sysUser.getSignature());
        resultMap.put("address", sysUser.getAddress());
        resultMap.put("phone", sysUser.getPhone());
        resultMap.put("createTime", sdf.format(sysUser.getCreateTime()));
        resultMap.put("sex", sysUser.getSex());
        return ResponseData.success(200, "获取用户基础信息成功", resultMap);
    }

}

